﻿using System;
using System.Windows.Data;

namespace Notoric.Gui.Converters
{
    /// <summary>
    /// Converts between the state of a toggle button and the value that correspond to this state.
    /// </summary>
    public class ToggleButtonConverter : IValueConverter
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ToggleButtonConverter"/> class.
        /// </summary>
        /// <param name="checkedValue">
        /// <para>
        /// Type: <see cref="Object"/>
        /// </para>
        /// <para>
        /// The value that corresponds to checked toggle button.
        /// </para>
        /// </param>
        /// <param name="uncheckedValue">
        /// <para>
        /// Type: <see cref="Object"/>
        /// </para>
        /// <para>
        /// The value that corresponds to unchecked toggle button.
        /// </para></param>
        public ToggleButtonConverter(object checkedValue, object uncheckedValue)
        {
            this.checkedValue = checkedValue;
            this.uncheckedValue = uncheckedValue;
        }

        #endregion

        #region IValueConverter Members

        /// <inheritdoc/>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is bool)
            {
                var boolVal = (bool)value;
                if (boolVal)
                    return checkedValue;
                else
                    return uncheckedValue;
            }
            else
                return null;
        }

        /// <inheritdoc/>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == checkedValue)
                return true;
            else
                return false;
        }

        #endregion

        #region Private Fields

        /// <summary>
        /// Holds the value that will be returned when the button is checked.
        /// </summary>
        private object checkedValue;
        /// <summary>
        /// Holds the value that will be returned when the button is not checked.
        /// </summary>
        private object uncheckedValue;

        #endregion
    }
}
